package cn.com.flower.passport.controller;

import cn.com.flower.common.pojo.authentication.CurrentPrincipal;
import cn.com.flower.common.web.JsonResult;
import cn.com.flower.passport.dao.cache.IUserCacheRepository;
import cn.com.flower.passport.pojo.param.UserLoginInfoParam;
import cn.com.flower.passport.pojo.vo.UserLoginResultVO;
import cn.com.flower.passport.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Api(tags = "01.登录模块")
@Validated
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private IUserService iUserService;

    @PostMapping("login")
    @ApiOperation(value = "用户登录")
    public JsonResult login(@Valid UserLoginInfoParam userLoginInfoParam,
                            @ApiIgnore HttpServletRequest request) {
        String remoteAddr = request.getRemoteAddr();
        String header = request.getHeader("User-Agent");
        UserLoginResultVO loginResultVO = iUserService.login(userLoginInfoParam, remoteAddr, header);
        return JsonResult.ok(loginResultVO);
    }

    @PostMapping("logout")
    @ApiOperation(value = "用户退出")
    public JsonResult logout(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal) {
        iUserService.logout(currentPrincipal);
        return JsonResult.ok();
    }
}
